package com.mallcai.bigdata.ladon.service.peekdata.impl;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Model;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.ModelMeta;
import com.mallcai.bigdata.ladon.dpl.mapper.peekdata.ModelMapper;
import com.mallcai.bigdata.ladon.dpl.mapper.peekdata.ModelMetaMapper;
import com.mallcai.bigdata.ladon.service.peekdata.IModelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-05-22 14:29.
 * Modify:
 */
@Service
@Slf4j
public class ModelService implements IModelService {
    @Autowired
    private ModelMapper     modelMapper;
    @Autowired
    private ModelMetaMapper modelMetaMapper;


    @Transactional
    @Override
    public int addModelInfo(Model model, List<ModelMeta> fields) {
        Assert.isTrue(StringUtils.isNotBlank(model.getName()), "模型名称为空");

        List<Model> models = modelMapper.getByName(model.getName());
        if (models != null && models.size() > 0) {
            throw new BusinessException("存在相同名称的模型");
        }

        int status = modelMapper.insert(model);
        if (status <= 0) {
            throw new BusinessException("添加模型失败");
        }
        Long modelId = model.getId();
        for (ModelMeta mm : fields) {
            mm.setModelId(modelId);
            modelMetaMapper.insert(mm);
        }

        return 1;
    }

    @Transactional
    @Override
    public int editModelInfo(Model model, List<ModelMeta> fields) {
        return editModel(model, fields);
    }

    @Transactional
    @Override
    public int upgradeModelInfo(Model model, List<ModelMeta> fields) {
        return editModel(model, fields);
    }

    private int editModel(Model model, List<ModelMeta> fields){
        Assert.notNull(model.getId(), "模型Id为空");
        Assert.isTrue(StringUtils.isNotBlank(model.getName()), "模型名称为空");

        List<Model> models = modelMapper.getByName(model.getName());
        if (models != null && models.size() > 0) {
            for (Model m : models) {
                if (!model.getId().equals(m.getId())) {
                    throw new BusinessException("存在相同名称的模型");
                }
            }
        }
        int status = modelMapper.update(model);
        if (status > 0) {
            for (ModelMeta mm : fields) {
                mm.setModelId(model.getId());
                if(mm.getId() != null){
                    modelMetaMapper.update(mm);
                } else {
                    modelMetaMapper.insert(mm);
                }
            }
        }
        return status;
    }



    @Override
    public int changeStatus(Model model) {
        return modelMapper.update(model);
    }

    @Transactional
    @Override
    public int deleteModelInfo(Model model) {
        Long modelId = model.getId();
        Assert.notNull(modelId, "模型Id为空");

        int status = modelMapper.delete(model);
        if (status <= 0) {
            throw new BusinessException("删除模型失败");
        }
        modelMetaMapper.delByModelId(modelId);
        return 1;
    }

    @Override
    public Model getById(Long modelId) {
        Model m = modelMapper.getById(modelId);
        if (m != null) {
            List<ModelMeta> fields = modelMetaMapper.getByModelId(m.getId());
            m.setFields(fields);
        }
        return m;
    }

    @Override
    public List<Model> getByParams(Map<String, Object> params) {
        return modelMapper.getByParams(params);
    }

    @Override
    public List<Model> getAllModels() {
        return modelMapper.getAllModels();
    }

    @Override
    public List<ModelMeta> listMetaByModelId(Long modelId) {
        return modelMetaMapper.getByModelId(modelId);
    }

    /**
     * 获取模型字段分组
     *
     * @param modelId Long
     * @return List
     */
    @Override
    public List<String> getColumnsGroupByModelId(Long modelId) {
        return modelMetaMapper.getColumnsGroupByModelId(modelId);
    }


    @Override
    public Model readModel(Long modelId) {
        Model model = modelMapper.getById(modelId);
        if(model != null) {
            model.setFields(modelMetaMapper.getByModelId(modelId));
        }
        return model;
    }
}

